#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>



using namespace std;

static void DealEvent(const SDL_Event *event);


#ifdef __WINDOWS__
int WINAPI WinMain(
  void* hInstance,
  void* hPrevInstance,
  void* lpCmdLine,
  int   nShowCmd
)
#else
int main(int argc, char const *argv[])
#endif
{
    const int16_t window_width  = 640;
    const int16_t window_height = 480;
    // 创建 window 指针
    SDL_Window* window = NULL;
    // 初使化 SDL. 如果返回 -1 说明初使化失败.
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
        cout << "Error initializing SDL: " << SDL_GetError() << endl;
        return 1;
    }
    // 创建窗口
    window = SDL_CreateWindow( "SDL2 Example",
                                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                window_width, window_height,
                                SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE );
    // 确保创建窗口成功
    if ( !window ) {
        cout << "Error creating window: " << SDL_GetError() << endl;
        return 1;
    }

    SDL_Surface *pic_suface;
    SDL_Texture *texture_spirit_00;
    SDL_Texture *texture_bg;
    SDL_Renderer *render_main;
    SDL_Rect rect;
    // 为一个窗口创建一个2D渲染环境。
    render_main = SDL_CreateRenderer(window,-1,0);
    //加载bmp
    pic_suface = SDL_LoadBMP("img/spirit_00.bmp");
    //bmp加载到纹理
    texture_spirit_00 = SDL_CreateTextureFromSurface(render_main,pic_suface);

    pic_suface = SDL_LoadBMP("img/bg.bmp");
    texture_bg = SDL_CreateTextureFromSurface(render_main,pic_suface);

    SDL_FreeSurface(pic_suface);

    bool quit = false;
    SDL_Event event;
    //主消息循环
    while (!quit)
    {
        // SDL_PollEvent(&event);
        SDL_WaitEvent(&event);
        if(event.type == SDL_QUIT)
        {
            quit = true;
        }
        else
        {
            DealEvent(&event);
        }
        SDL_RenderClear(render_main);
        SDL_RenderCopy(render_main,texture_bg,NULL,NULL);
        rect.x = 50;
        rect.y = 50;
        rect.w = 80;
        rect.h = 80;
        SDL_RenderCopy(render_main,texture_spirit_00,NULL,&rect);
        rect.x = 250;
        rect.y = 50;
        rect.w = 80;
        rect.h = 80;
        SDL_RenderCopy(render_main,texture_spirit_00,NULL,&rect);
        SDL_RenderPresent(render_main);
    }
    SDL_DestroyTexture(texture_spirit_00);
    SDL_DestroyTexture(texture_bg);
    SDL_DestroyRenderer(render_main);
    // 销毁 window。这也会销毁 surface
    SDL_DestroyWindow( window );
    // 退出 SDL
    SDL_Quit();
    cout << "SDL Quit" << endl;
    return 0;
}


static void DealEvent(const SDL_Event *event)
{
    switch (event->type)
    {
        /* Application events */
        case SDL_QUIT:                      /**< User-requested quit */
        {
        }
        break;

        case SDL_LOCALECHANGED:             /**< The user's locale preferences have changed. */
        {
        }
        break;

        /* Display events */
        case SDL_DISPLAYEVENT:              /**< Display state change */
        {
        }
        break;

        /* Window events */
        case SDL_WINDOWEVENT:               /**< Window state change */
        {
        }
        break;
        case SDL_SYSWMEVENT:                /**< System specific event */
        {
        }
        break;

        /* Keyboard events */
        case SDL_KEYDOWN:                   /**< Key pressed */
        {
            cout << "SDL_KEYDOWN :" << event->key.keysym.sym << endl;
        }
        break;
        case SDL_KEYUP:                     /**< Key released */
        {
        }
        break;
        case SDL_TEXTEDITING:               /**< Keyboard text editing (composition) */
        {
        }
        break;
        case SDL_TEXTINPUT:                 /**< Keyboard text input */
        {
        }
        break;
        case SDL_KEYMAPCHANGED:             /**< Keymap changed due to a system event such as an input language or keyboard layout change.*/
        {
        }
        break;

        /* Mouse events */
        case SDL_MOUSEMOTION:               /**< Mouse moved */
        {
        }
        break;
        case SDL_MOUSEBUTTONDOWN:           /**< Mouse button pressed */
        {
        }
        break;
        case SDL_MOUSEBUTTONUP:             /**< Mouse button released */
        {
        }
        break;
        case SDL_MOUSEWHEEL:                /**< Mouse wheel motion */
        {
        }
        break;

        /* Render events */
        case SDL_RENDER_TARGETS_RESET:      /**< The render targets have been reset and their contents need to be updated */
        {
        }
        break;
        case SDL_RENDER_DEVICE_RESET:       /**< The device has been reset and all textures need to be recreated */
        {
        }
        break;

        /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use,
         *  and should be allocated with SDL_RegisterEvents()
         */
        case SDL_USEREVENT:
        {
        }
        break;
    }
}
